﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SportStore.Models.Entities;
using SportStore.Models.Abstract;
using System.Drawing;
using System.Data.Objects;

namespace SportStore.Models.Concrete
{
    public class EFRepository : IRepository
    {
        public EFRepository()
        {
            context.Database.CreateIfNotExists();
        }

        public EFDbContext context = new EFDbContext();

        public IQueryable<Product> Products
        {
            get { return context.Products; }
        }

        public IQueryable<Category> Categories
        {
            get { return context.Categories; }
        }

        public void SaveProduct(Product product)
        {
            if (product.ProductID == 0)
            {
                context.Products.Add(product);
            }
            else
            {
                context.Entry<Product>(product).State = System.Data.EntityState.Modified;
            }
            context.SaveChanges();
        }

        

        public void DeleteProduct(Product product)
        {
            context.Products.Remove(product);
            context.SaveChanges();
        }

        public void SaveCategory(Category category)
        {
            if (category.CategoryID == 0)
            {
                context.Categories.Add(category);
            }
            else
            {
                context.Entry(category).State = System.Data.EntityState.Modified;
            }
            context.SaveChanges();
        }

        public void DeleteCategory(Category category)
        {
            context.Categories.Remove(category);
            context.SaveChanges();
        }

        public void DeleteCategory(Product product)
        {
            context.Products.Remove(product);
            context.SaveChanges();
        }


        public IQueryable<User> Users
        {
            get { return context.Users; }
        }

        public void SaveUser(User user)
        {
            if (user.UserID == 0)
            {
                context.Users.Add(user);
            }
            else
            {
                context.Entry<User>(user).State = System.Data.EntityState.Modified;
            }
            context.SaveChanges();
        }

        public void DeleteUser(User user)
        {
            context.Users.Remove(user);
            context.SaveChanges();
        }

        public IQueryable<Role> Roles
        {
            get { return context.Roles; }
        }

        public void SaveRole(Role role)
        {
            if (role.RoleID == 0)
            {
                context.Roles.Add(role);
            }
            else
            {
                context.Entry<Role>(role).State = System.Data.EntityState.Modified;
            }
            context.SaveChanges();
        }

        public void DeleteRole(Role role)
        {
            context.Roles.Remove(role);
            context.SaveChanges();
        }
    }
}